package com.fenghongzhang.lib_core.state

import com.fenghongzhang.lib_core.base.BaseActivity
import com.fenghongzhang.lib_core.utils.ToastUtils

/**
 *@author Wcj
 *@description
 *@date 2022/7/14 9:42
 */
sealed class RequestEvent

object Success : RequestEvent()
object Default : RequestEvent()
class Failed(val message: String) : RequestEvent()
object Loading : RequestEvent()
object Empty : RequestEvent()

fun RequestEvent.toUIStatus(): UIState? {
    return when (this) {
        is Loading -> UIState.LOADING
        is Success -> UIState.SUCCESS
        is Empty -> UIState.EMPTY
        is Failed -> UIState.NETWORK_ERROR
        else -> null
    }
}

fun RequestEvent.attachToLoadingView(baseActivity: BaseActivity<*, *>, onSuccess: (() -> Unit)? = null, onFailed: ((RequestEvent) -> Unit)? = null) {
    when (this) {
        is Loading -> baseActivity.setLoadingViewVisible(true)
        is Success -> {
            baseActivity.setLoadingViewVisible(false)
            onSuccess?.invoke()
        }
        is Empty -> baseActivity.setLoadingViewVisible(false)
        is Failed -> {
            baseActivity.setLoadingViewVisible(false)
            ToastUtils.showShort(baseActivity, this.message)
            onFailed?.invoke(this)
        }
        else ->
            baseActivity.setLoadingViewVisible(false)

    }
}